package com.cms.server.resolver.plugins.sctz;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import com.efounder.builder.base.data.DataSetUtils;
import com.efounder.builder.base.data.EFDataSet;
import com.efounder.builder.base.data.EFRowSet;
import com.efounder.eai.data.JParamObject;
import com.efounder.form.EFFormDataModel;
import com.efounder.form.io.FormDataResolverAdapter;
import com.efounder.form.server.resolver.util.SYS_MDL_VAL;
import com.efounder.sql.JStatement;
import com.metadata.bizmodel.SYS_MODEL;
import com.persistence.FLOW_TASK_LIST;


/**
 * <p>Title: 节点自动提交</p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2014</p>
 * @author prayer
 * @version 1.0
 */
public class CMSAutoSubmit extends FormDataResolverAdapter {
	//初始化FormDataModel
	public void finishSaveForm(JStatement stmt, EFFormDataModel model, JParamObject PO) throws Exception {
		EFDataSet                    billDataSet = model.getBillDataSet();
		EFRowSet                      nodeRowset = EFRowSet.getInstance();
		EFRowSet                      flowRowset = EFRowSet.getInstance();
		EFRowSet                  nextNodeRowset = EFRowSet.getInstance();
		String                            strSql = "";
		long                             curTime = new Date().getTime();
		ResultSet                             rs = null;
		String                            F_KJQJ = billDataSet.getRowSet(0).getString(PO.GetValueByParamName(SYS_MODEL._BLFL_KJQJ_COL_, SYS_MDL_VAL.BILL_KJQJ), "");
		String                            F_GUID = billDataSet.getRowSet(0).getString(PO.GetValueByParamName(SYS_MODEL._BILL_GUID_COL_, SYS_MDL_VAL.BILL_GUID), "");
		String                            F_DJBH = billDataSet.getRowSet(0).getString(PO.GetValueByParamName(SYS_MODEL._BILL_BH_COL_, SYS_MDL_VAL.BILL_BH), null);
		String                           FLOW_ID = PO.GetValueByParamName(FLOW_TASK_LIST._FLOW_ID, "");
		String                             OP_ID = PO.GetValueByParamName(FLOW_TASK_LIST._OP_ID, "");
		String                           NODE_ID = PO.GetValueByParamName(FLOW_TASK_LIST._OP_NODE, "");
		String                     RESR_IN_CAUSE = PO.GetValueByParamName(FLOW_TASK_LIST._RESR_IN_CAUSE, "");
		String                       RESR_STATUS = PO.GetValueByParamName(FLOW_TASK_LIST._RESR_STATUS + "_BACK", "");//从会签节点提交到下一个节点，并且下一个节点为自动提交节点，所以把之前的状态存储起来
		
		if(!RESR_IN_CAUSE.equals("submit")) return;
		
		
		//如果为空，则说明上个节点不是会签节点，还是在PO里RESR_STATUS取出来
		if(RESR_STATUS.trim().equals("")) {
			RESR_STATUS = PO.GetValueByParamName(FLOW_TASK_LIST._RESR_STATUS, "");
		}
		strSql = "select B.*, A.NODE_ID as NODE_PRE_ID from FLOW_NODE_LIST A, FLOW_NODE_LIST B where A.FLOW_ID ='" + FLOW_ID + "' and A.NODE_ID = '" + NODE_ID + "' and "
		       + " A.FLOW_ID = B.FLOW_ID and A.NODE_NEXT = B.NODE_ID";
		rs = stmt.executeQuery(strSql);
		if(rs != null && rs.next()) nodeRowset = (EFRowSet) DataSetUtils.resultSet2RowSet(rs, nodeRowset);
		
		strSql = "select * from FLOW_TASK_LIST where FLOW_ID ='" + FLOW_ID + "' and OP_ID = '" + OP_ID + "'";
		rs = stmt.executeQuery(strSql);
		if(rs != null && rs.next()) flowRowset = (EFRowSet) DataSetUtils.resultSet2RowSet(rs, flowRowset);
		else flowRowset = null;
	
		strSql = "select * from FLOW_NODE_LIST where FLOW_ID ='" + FLOW_ID + "' and NODE_ID = '" + nodeRowset.getString("NODE_NEXT", "") + "'";
		rs = stmt.executeQuery(strSql);
		if(rs != null && rs.next()) {
			nextNodeRowset = (EFRowSet) DataSetUtils.resultSet2RowSet(rs, nextNodeRowset);
		}
		
		strSql = "select * from FLOW_TASK_LIST where FLOW_ID ='" + FLOW_ID + "' and NODE_TAG = '" + nodeRowset.getString("NODE_ID", "") + "' and RESR_STATUS = 'pending'";
		rs = stmt.executeQuery(strSql);

		if(rs != null && rs.next() && nodeRowset.getString("NODE_AUTO_SUBMIT", "").equals("1") && RESR_IN_CAUSE.equals("submit") && flowRowset != null) {
			//更新任务节点的状态为结束
			strSql = " update FLOW_TASK_LIST set RESR_STATUS = 'processed', RESR_OUT_CAUSE = 'submit' "
				   + " where OP_ID = '" + OP_ID + "' and FLOW_ID = '" + FLOW_ID + "'";
			stmt.execute(strSql);
			
			//任务信息更新到结束流程信息里
			strSql = "insert into FLOW_TASK_LIST (OP_ID,FLOW_ID,BIZ_DJBH,BIZ_DATE,BIZ_GUID,OP_USER,OP_USER_NAME,OP_TIME,"
				   + "OP_BMBH,OP_NOTE,TASK_STATUS,OP_NODE,NODE_SRC,NODE_SRC_NAME,NODE_TAG,NODE_TAG_NAME,POP_ID,RESR_STATUS,TASK_TO_UNIT,TASK_TO_UNIT_NAME," ;
		 	
			for(int j = 1; j <= 20; j++) {
				if(j < 10) {
					if(PO.GetValueByParamName(FLOW_TASK_LIST._F_CHAR + "0" + j, null) != null) {
						strSql += "F_CHAR0" + j + ",";
					}
					if(PO.GetValueByParamName(FLOW_TASK_LIST._F_DATE + "0" + j, null) != null) {
						strSql += "F_DATE0" + j + ",";
					}
					if(PO.GetValueByParamName(FLOW_TASK_LIST._F_NUM + "0" + j, null) != null) {
						strSql += "F_NUM0" + j + ",";
					}
				} else {
					if(PO.GetValueByParamName(FLOW_TASK_LIST._F_CHAR + j, null) != null) {
						strSql += "F_CHAR" + j + ",";
					}
					if(PO.GetValueByParamName(FLOW_TASK_LIST._F_DATE + j, null) != null) {
						strSql += "F_DATE" + j + ",";
					}
					if(PO.GetValueByParamName(FLOW_TASK_LIST._F_NUM + j, null) != null) {
						strSql += "F_NUM" + j + ",";
					}
				}
			}
		 	strSql += "RESR_IN_CAUSE,RESR_OUT_CAUSE) values ('"
		 	        + curTime + "', '" + PO.GetValueByParamName(FLOW_TASK_LIST._FLOW_ID, "") + "', '"
					+ F_DJBH + "', '" + F_KJQJ + "', '" + F_GUID + "', '" + PO.GetValueByParamName(FLOW_TASK_LIST._OP_USER, "")
					+ "', '" + PO.GetValueByParamName(FLOW_TASK_LIST._OP_USER_NAME, "") + "', '"
					+ curTime + "', '" + PO.GetValueByParamName(FLOW_TASK_LIST._OP_BMBH, "") + "', '" 
					+ PO.GetValueByParamName(FLOW_TASK_LIST._OP_NOTE, "") + "', 'processing', '"
					+ nodeRowset.getString("NODE_ID", "") + "', '" + nodeRowset.getString("NODE_ID", "") + "', '"
					+ nodeRowset.getString("NODE_NAME", "") + "', '" + nextNodeRowset.getString("NODE_ID", "") + "', '" + nextNodeRowset.getString("NODE_NAME", "") 
					+ "', '" + flowRowset.getString("OP_ID", "") + "', '" + RESR_STATUS
					+ "','" + PO.GetValueByParamName(FLOW_TASK_LIST._TASK_TO_UNIT, "") + "','" + PO.GetValueByParamName(FLOW_TASK_LIST._TASK_TO_UNIT_NAME, "") + "',";
		 	for(int j = 1; j <= 20; j++) {
		 		if(j < 10) {
		 			if(PO.GetValueByParamName(FLOW_TASK_LIST._F_CHAR + "0" + j, null) != null) {
		 				strSql += "'" + PO.GetValueByParamName(FLOW_TASK_LIST._F_CHAR + "0" + j, null)  + "',";
		 			} 
		 			if(PO.GetValueByParamName(FLOW_TASK_LIST._F_DATE + "0" + j, null) != null) {
		 				strSql += "'" + PO.GetValueByParamName(FLOW_TASK_LIST._F_DATE + "0" + j, null)  + "',";
		 			}
		 			if(PO.GetValueByParamName(FLOW_TASK_LIST._F_NUM + "0" + j, null) != null) {
		 				strSql += "'" + PO.GetValueByParamName(FLOW_TASK_LIST._F_NUM + "0" + j, null)  + "',";
		 			}
		 		} else {
		 			if(PO.GetValueByParamName(FLOW_TASK_LIST._F_CHAR + j, null) != null) {
		 				strSql += "'" + PO.GetValueByParamName(FLOW_TASK_LIST._F_CHAR + j, null)  + "',";
		 			} 
		 			if(PO.GetValueByParamName(FLOW_TASK_LIST._F_DATE + j, null) != null) {
		 				strSql += "'" + PO.GetValueByParamName(FLOW_TASK_LIST._F_DATE + j, null)  + "',";
		 			}
		 			if(PO.GetValueByParamName(FLOW_TASK_LIST._F_NUM + j, null) != null) {
		 				strSql += "'" + PO.GetValueByParamName(FLOW_TASK_LIST._F_NUM + j, null)  + "',";
		 			}
		 		}
		 	}
		 	strSql += "'" + PO.GetValueByParamName(FLOW_TASK_LIST._RESR_IN_CAUSE, "") + "', '')";
		 	PO.SetValueByParamName(FLOW_TASK_LIST._OP_ID, String.valueOf(curTime));
		 	PO.SetValueByParamName(FLOW_TASK_LIST._OP_NODE, nodeRowset.getString("NODE_ID", ""));
		 	PO.SetValueByParamName("OP_NODE_NEXT", nextNodeRowset.getString("NODE_ID", ""));
			stmt.execute(strSql);
		}
	}
}
